package tt.dz.appserver.service.business.imp;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.avro.AvroRemoteException;
import org.apache.log4j.Logger;
import org.mortbay.log.Log;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import tt.dz.appserver.dao.ext.StationCollectionDao;
import tt.dz.appserver.dao.ext.StationCommentDao;
import tt.dz.appserver.dao.ext.StationCommentReplyDao;
import tt.dz.appserver.dao.ext.ThumbUpDao;
import tt.dz.appserver.dao.ext.UserInfoDao;
import tt.dz.appserver.entity.model.ResultMappingEntity;
import tt.dz.appserver.entity.model.StationCollection;
import tt.dz.appserver.entity.model.StationComment;
import tt.dz.appserver.entity.model.pub.ThumbUp;
import tt.dz.appserver.entity.model.user.UserInfo;
import tt.dz.appserver.entity.vo.StationCollectionVO;
import tt.dz.appserver.entity.vo.station.StationCommentInfoVo;
import tt.dz.appserver.entity.vo.station.StationCommentReplyInfoVo;
import tt.dz.appserver.entity.vo.station.StationDetailVo;
import tt.dz.appserver.entity.vo.station.StationInfoVo;
import tt.dz.appserver.entity.vo.user.UserBaseVo;
import tt.dz.appserver.service.avro.client.oms.dzttOmsServiceProtocol;
import tt.dz.appserver.service.business.StationService;
import tt.dz.appserver.util.CalculateUtil;
import tt.dz.appserver.util.CodeUtils;
import tt.dz.appserver.util.Constants;
import tt.dz.appserver.util.Result;
import tt.dz.appserver.util.StationOpUtils;
import tt.dz.appserver.util.date.DateUtil;
import tt.dz.appserver.util.dzcode.OmsCode;
import tt.dz.appserver.util.encoding.EncodingUtils;
import tt.dz.appserver.util.enums.Response;
import tt.dz.appserver.util.enums.StationReviewStatus;
import tt.dz.appserver.util.enums.ThumbUpType;
import tt.dz.appserver.util.enums.collectionFlag;
import tt.dz.appserver.util.enums.isBooking;
import tt.dz.appserver.util.json.JsonUtils;
import tt.dz.appserver.entity.model.pub.station.StationCommentReply;

/**
 * 站点接口实现类
 * 
 * @author xielf
 *
 */
public class StationServiceImpl implements StationService {

	/** 日志 */
	private static final Logger LOG = Logger.getLogger("[站点接口]:");

	/** OMS数据访问接口 */
	@Resource(name = "omsMessageProtocolRequestor")
	private dzttOmsServiceProtocol omsService;

	/** 站点收藏Dao */
	@Resource
	private StationCollectionDao stationCollectionDao;

	@Resource
	private StationCommentDao stationCommentDao;
	
	@Resource
	private UserInfoDao userInfoDao;
	/**点赞信息*/
	@Resource
	private ThumbUpDao thumbUpDao;
	
	@Resource
	private StationCommentReplyDao stationCommentReplyDao;

	@Cacheable(value="station")
	@Override
	public ResultMappingEntity getStationDataPackage() throws Exception {

		ResultMappingEntity resultMappingEntity = null;

		try {
			LOG.info("读取所有的站点信息");
			List<StationInfoVo> stationDetailVos = StationOpUtils.readStationObject();
			//   chargePortType 充电接口类型：1.国标；2特斯拉； 3.ABB， 4其他
			//  operMerchantId 充电运营服务商ID
			// stationType  站点类型 1直流2交流3混合
			//如果传人条件不为空，这才需要验证
			
			for(StationInfoVo vo:stationDetailVos){
				vo.setGrade("5");
				if(vo.getScore()==null){
					vo.setScore("5");
				}
				//String chargePortType=vo.getChargePortType();
				vo.setChargePortType("0001");
				/*if(chargePortType==null||chargePortType.equals("")||chargePortType.equals("4")){
					chargePortType="0";
					//vo.setChargePortType("0000");	
					//TODO 目前只有国标 去1 级0001
					vo.setChargePortType("0001");
				}else{
					String type=Integer.toBinaryString((int)Math.pow(2,Integer.parseInt(chargePortType)));
					type=tt.dz.appserver.util.StringUtils.fill(type,4) ;
					vo.setChargePortType(type);	
				}	*/
			}
			
			
			// 将封装后的集合加入需要返回的json对象中
			resultMappingEntity = Result.responseBody(Response.SUCCESS.getCode(), Response.SUCCESS.getMsg(),stationDetailVos);
		} catch (Exception e) {
			throw e;
		}
		return resultMappingEntity;
	}
	
	
	public static void main(String[] args) {
		String type=				
				Integer.toBinaryString((int)Math.pow(2,Integer.parseInt("2")));
		System.out.println(type);
	}
	@Override
	public JSONObject updateStationDataPackage(String provinceId) throws Exception {

		JSONObject resultJsonObject = null;

		try {
			JSONArray resultDataList = StationOpUtils.readStationObjectByProvinceId(provinceId);
			// 将获取后的集合加入需要返回的json对象中
			resultJsonObject = JsonUtils.getSuccessJson();
			resultJsonObject.put("stationList", resultDataList);
		} catch (Exception e) {
			throw e;
		}
		return resultJsonObject;
	}

	@Override
	public JSONObject collectionStation(String userId, String stationId) throws AvroRemoteException {

		JSONObject resultJsonObject = null;

		boolean isCollection = stationCollectionDao.stationIsCollection(stationId, userId);
		// 如果已经收藏则删除
		if (isCollection) {

			stationCollectionDao.deleteStationCollection(stationId, userId);
			resultJsonObject = JsonUtils.getSuccessJson();
		}
		// 未收藏
		else {

			// 获取站点详细
			String resultJsonString = omsService.getStationDetailByStationId(stationId).toString();

			// 数据转换
			JSONObject omsResultObject = JSONObject.parseObject(resultJsonString);

			// 获取code
			String code = omsResultObject.getString(Constants.OmsServerResultField.CODE);

			if (OmsCode.SUCCESS.equals(code)) {

				// 获取站点详细
				JSONObject stationObject = omsResultObject.getJSONObject("pojo");

				String stationName = stationObject.getString("station_name");
				String address = stationObject.getString("address");

				// 站点收藏实体
				StationCollection stationCollection = new StationCollection();
				stationCollection.setColl_time(new Date());
				stationCollection.setStation_id(stationId);
				stationCollection.setUser_id(userId);
				stationCollection.setStation_addr(address);
				stationCollection.setStation_name(stationName);
				// 保存对象
				stationCollectionDao.save(stationCollection);
				resultJsonObject = JsonUtils.getSuccessJson();
			} else {
				Response response = CodeUtils.getCodeByOMSServerCode(code);
				resultJsonObject = JsonUtils.getFailJson(response);
			}
		}

		return resultJsonObject;
	}

	@Override
	public JSONObject getStationDetailByStationId(String stationId, String userId) throws AvroRemoteException {

		JSONObject resultJsonObject = null;

		String resultJsonString = omsService.getStationDetailByStationId(stationId).toString();

		LOG.info("获取 【" + stationId + "】 站点详细,OMS返回结果:" + resultJsonString);

		// json转换
		JSONObject omsResultObject = JSONObject.parseObject(resultJsonString);

		String code = omsResultObject.getString(Constants.OmsServerResultField.CODE);

		if (OmsCode.SUCCESS.equals(code)) {

			// 获取站点详细
			JSONObject stationObject = omsResultObject.getJSONObject("pojo");

			// 封装站点字段
			StationDetailVo stationDetailVo = JsonUtils.JsonToObjectOfClass(stationObject, StationDetailVo.class);
			String photo = stationObject.getString("photo");
			stationDetailVo.setStationImages(photo);
			if(stationObject!=null){
				
				JSONArray  locationList=stationObject.getJSONArray("locationList");
				if(locationList!=null&&locationList.size()>0){
					JSONObject location=(JSONObject) locationList.get(0);
					String coordinate_x = location.getString("coordinate_x");
					String coordinate_y = location.getString("coordinate_y");
					stationDetailVo.setLatitude(coordinate_y);
					stationDetailVo.setLongitude(coordinate_x);
				}
				
			}
			if(stationDetailVo.getScore()==null){
				stationDetailVo.setScore("5");
			}
			if(stationDetailVo.getGrade()==null)
				stationDetailVo.setGrade(stationDetailVo.getScore());
			// String grade = stationCommentDao.findStationCommentGrade(stationId);
			else
			stationDetailVo.setGrade("5");
			
			
			

			if (StationReviewStatus.PASS.getCode().equals(stationDetailVo.getReviewState())) {

				// 是否可预约 1可以 0不可以
				stationDetailVo.setBooking(isBooking.YES.getValue());
			} else {

				// 是否可预约 1可以 0不可以
				stationDetailVo.setBooking(isBooking.NO.getValue());
			}
			//查询该站点的费率

			/** 其他操作 */
			// 是否收藏
			boolean bool = false;
			if (!StringUtils.isEmpty(userId)) {
				bool = stationCollectionDao.stationIsCollection(stationId, userId);
			}
			// 设置收藏标记
			stationDetailVo.setCollectionFlag(bool ? collectionFlag.YES.getValue() : collectionFlag.NO.getValue());

			resultJsonObject = JsonUtils.getSuccessJson();
			resultJsonObject.put("stationInfo", stationDetailVo);
		} else {
			Response response = CodeUtils.getCodeByOMSServerCode(code);
			resultJsonObject = JsonUtils.getFailJson(response);
		}

		return resultJsonObject;
	}

	@Override
	public StationCommentInfoVo stationComment(String stationId, String userId, String content, Integer grade,
			String address) {

		// 评论对象
		StationComment stationComment = new StationComment();
		stationComment.setStation_id(stationId);
		stationComment.setComment_grade(grade);
		stationComment.setComment_time(new Date());
		stationComment.setContent(content);
		stationComment.setUser_id(userId);
		stationComment.setComment_addr(address);
		stationComment.setAudit_flag(1);
		stationComment.setComment_id(DateUtil.getDateTimeLong()+EncodingUtils.getRandomIntegerString(4));
		stationCommentDao.save(stationComment);

		// 客户端展示
		StationCommentInfoVo stationCommentVo = new StationCommentInfoVo();

		stationCommentVo.setAddress(stationComment.getComment_addr());
		stationCommentVo.setCommentTime(DateUtil.getDateTime(stationComment.getComment_time()));
		stationCommentVo.setGrade(stationComment.getComment_grade().toString());
		stationCommentVo.setStationId(stationId);
		stationCommentVo.setUserId(stationComment.getUser_id());
		stationCommentVo.setAuditFlag(stationComment.getAudit_flag().toString());
		//刚完成评论 没有调用点赞接口
		stationCommentVo.setIsThumbUp("false");
		stationCommentVo.setThumbUpNum(0);
		stationCommentVo.setCommentId(stationComment.getComment_id());
		stationCommentVo.setContent(stationComment.getContent());
		//
		LOG.info("用户评论id="+stationComment.getComment_id()+"userid="+userId);
		UserInfo userInfo = userInfoDao.findUserByUserId(userId);
		UserBaseVo userBaseVo = new UserBaseVo();
		
		//设置返回用户信息
		userBaseVo = setUserbasVo(userInfo);
		stationCommentVo.setUser(userBaseVo);
		
		return stationCommentVo;
	}

	@Override
	public List<StationCommentInfoVo> stationCommentList(String stationId, Integer pageNo,String userId) {

		// 评论列表
		List<StationComment> stationComments = stationCommentDao.getStationCommentByStationId(stationId, pageNo);
		Log.info("点赞评论列表传入参数stationId="+stationId+";pageNo="+pageNo+";userId="+userId+"获取到的评论条数="+stationComments.size()+";");
		
		List<StationCommentInfoVo> stationCommentVos = new ArrayList<StationCommentInfoVo>();
		
		
		for (StationComment stationComment : stationComments) {

			StationCommentInfoVo stationCommentVo = new StationCommentInfoVo();
			stationCommentVo.setAddress(stationComment.getComment_addr());
			stationCommentVo.setCommentTime(DateUtil.getDateTime(stationComment.getComment_time()));
			stationCommentVo.setGrade(stationComment.getComment_grade().toString());
			stationCommentVo.setStationId(stationId);
			//stationCommentVo.setUserId(stationComment.getUser_id());
			stationCommentVo.setCommentId(stationComment.getComment_id());
			stationCommentVo.setContent(stationComment.getContent());
			stationCommentVo.setReplyCommentId(stationComment.getReply_comment_id());
			
			//查询用户信息
			LOG.info("获取到评论的用户id="+stationComment.getUser_id());
			UserInfo userInfo = userInfoDao.findUserByUserId(stationComment.getUser_id());
			List<ThumbUp> thumbs = thumbUpDao.findThumbUpByAssociatedTypeAndAssociatedId(ThumbUpType.STATION.getValue(), stationComment.getComment_id());
			LOG.debug("获取到的点赞数+【"+thumbs.size()+"】");
			stationCommentVo.setThumbUpNum(thumbs.size());	
			stationCommentVo.setIsThumbUp("false");
			if(userId==null||userId.equals("")){
				stationCommentVo.setIsThumbUp("false");
			}else{	
				for(ThumbUp up:thumbs){
					if((up.getUser_id()).equals(userId)){
						stationCommentVo.setIsThumbUp("true");
						break;
					}
				}
			}
			if(userInfo==null){			
				LOG.error("获取到评论的用户为空，评论用户的userid为="+stationComment.getUser_id());
				continue;
			}
	
			//展示用户V
			UserBaseVo userBaseVo = new UserBaseVo();
			
			//设置返回用户信息
			userBaseVo = setUserbasVo(userInfo);
			
			stationCommentVo.setUser(userBaseVo);
			
			if (stationComment.getAudit_flag() == 1) {
				stationCommentVo.setContent(stationComment.getContent());
			}
			stationCommentVo.setAuditFlag(stationComment.getAudit_flag().toString());
			//设置被回复对象
			if(stationComment.getReply_user_id()!=null&&(!stationComment.getReply_user_id().equals(""))){
				
				UserInfo replyUserInfo = userInfoDao.findUserByUserId(stationComment.getReply_user_id());
				UserBaseVo replyUserBaseVo = new UserBaseVo();
				
				//设置返回用户信息
				replyUserBaseVo = setUserbasVo(replyUserInfo);
				
				stationCommentVo.setReplyUser(replyUserBaseVo);
			}	
			
			
			stationCommentVos.add(stationCommentVo);
		}
		return stationCommentVos;
	}
	
	@Override
	public ResultMappingEntity stationThumbUp(String userId, String commentId) {
		
		LOG.info("点赞的用户id:"+userId+";站点评论id:"+commentId);
		if(userId==null||userId.trim()==""||commentId==null||commentId.trim()==""){
			LOG.info("点赞失败");
			return Result.responseBody("0", "用户未登录或者评论已过期");
		}
		ThumbUp thumbUp = thumbUpDao.findThumbUpByUserIdAndAssociatedTypeAndAssociatedId(userId,ThumbUpType.STATION.getValue(), commentId);
		if(thumbUp==null){
			thumbUp = new ThumbUp();
			thumbUp.setAssociated_id(commentId);
			thumbUp.setAssociated_type(ThumbUpType.STATION.getValue());
			Date date=new Date();
			thumbUp.setCreate_time(date);
			thumbUp.setUser_id(userId);
			thumbUpDao.save(thumbUp);
		}
		else{
			thumbUpDao.delete(thumbUp);
		}
		return Result.responseBody(Response.SUCCESS.getCode(), Response.SUCCESS.getMsg());
	}
	
	@Override
	public ResultMappingEntity stationCommentReplyThumbUp(String userId, String replyId) {
		
		LOG.info("点赞的用户id:"+userId+";站点评论回复id:"+replyId);
		if(userId==null||userId.trim()==""||replyId==null||replyId.trim()==""){
			LOG.info("点赞失败");
			return Result.responseBody("0", "用户未登录或者评论已过期");
		}
		ThumbUp thumbUp = thumbUpDao.findThumbUpByUserIdAndAssociatedTypeAndAssociatedId(userId,ThumbUpType.STATIONREPLY.getValue(), replyId);
		if(thumbUp==null){
			thumbUp = new ThumbUp();
			thumbUp.setAssociated_id(replyId);
			thumbUp.setAssociated_type(ThumbUpType.STATIONREPLY.getValue());
			Date date=new Date();
			thumbUp.setCreate_time(date);
			thumbUp.setUser_id(userId);
			thumbUpDao.save(thumbUp);
		}
		else{
			thumbUpDao.delete(thumbUp);
		}
		return Result.responseBody(Response.SUCCESS.getCode(), Response.SUCCESS.getMsg());
	}
	
	/**
	 * 站点评论回复
	 */
	@Override
	public ResultMappingEntity stationCommentReply(String userId, String commentId, String replyUserId, String content,
			String address,String stationId,int grade) {
		LOG.info("评论回复：用户id["+userId+"]评论回复id["+commentId+"]回复用户id["+replyUserId+"]");
		// 评论对象
				StationComment stationComment = new StationComment();
				stationComment.setStation_id(stationId);
				stationComment.setComment_time(new Date());
				stationComment.setContent(content);
				stationComment.setUser_id(userId);
				stationComment.setComment_addr(address);
				stationComment.setAudit_flag(1);
				stationComment.setReply_user_id(replyUserId);
				stationComment.setComment_id(DateUtil.getDateTimeLong()+EncodingUtils.getRandomIntegerString(4));
				stationComment.setReply_comment_id(commentId);
				stationComment.setComment_grade(5);
				stationCommentDao.save(stationComment);

				// 客户端展示
				StationCommentInfoVo stationCommentVo = new StationCommentInfoVo();

				stationCommentVo.setAddress(stationComment.getComment_addr());
				stationCommentVo.setCommentTime(DateUtil.getDateTime(stationComment.getComment_time()));
				stationCommentVo.setGrade(stationComment.getComment_grade().toString());
				stationCommentVo.setStationId(stationId);
				stationCommentVo.setUserId(stationComment.getUser_id());
				stationCommentVo.setAuditFlag(stationComment.getAudit_flag().toString());
				//刚完成评论 没有调用点赞接口
				stationCommentVo.setIsThumbUp("false");
				stationCommentVo.setThumbUpNum(0);
				stationCommentVo.setCommentId(stationComment.getComment_id());
				stationCommentVo.setContent(stationComment.getContent());
				//stationCommentVo.setReplyUserId(replyUserId);
				stationCommentVo.setReplyCommentId(commentId);
				//
				LOG.info("用户评论id="+stationComment.getComment_id()+"userid="+userId+"被回复用户id="+replyUserId);
				UserInfo userInfo = userInfoDao.findUserByUserId(userId);
				UserBaseVo userBaseVo = new UserBaseVo();
				
				userBaseVo = setUserbasVo(userInfo);
				
				stationCommentVo.setUser(userBaseVo);	
				UserInfo replyUserInfo = userInfoDao.findUserByUserId(replyUserId);
				UserBaseVo replyUserBaseVo = new UserBaseVo();
								
				replyUserBaseVo = setUserbasVo(replyUserInfo);
				
				stationCommentVo.setReplyUser(replyUserBaseVo);		
		return Result.responseBody(Response.SUCCESS.getCode(), Response.SUCCESS.getMsg(),stationCommentVo);
	}
	
	/**
	 * 删除评论
	 */
	public ResultMappingEntity deleteStationReply(String userId,String commentId,String replyId){
		
		StationCommentReply reply = new StationCommentReply();
		reply.setReply_id(replyId);
		reply.setComment_id(commentId);
		reply.setUser_id(userId);
		stationCommentReplyDao.delete(reply);
		return Result.responseBody(Response.SUCCESS.getCode(), Response.SUCCESS.getMsg()); 
	}


	@Override
	public List<StationCommentReplyInfoVo> getStationReplyByCommentId( String commentId,String userId,Integer pageNo) {
		
		// 评论列表
				List<StationCommentReply> stationCommentreplys = stationCommentReplyDao.getStationCommentByCommentId(commentId, pageNo);
				Log.info("站点平路传入参数commentId="+commentId+";pageNo="+pageNo+";userId="+userId+"获取到的回复条数="+stationCommentreplys.size()+";");
				
				List<StationCommentReplyInfoVo> stationCommentReplyInfoVos = new ArrayList<StationCommentReplyInfoVo>();
				
				
				for (StationCommentReply comment : stationCommentreplys) {

					StationCommentReplyInfoVo reply = new StationCommentReplyInfoVo();
					reply.setAddress(comment.getComment_addr());
					reply.setCommentTime(DateUtil.getDateTime(comment.getCreate_time()));
					//reply.setGrade("");
					reply.setCommentId(comment.getComment_id());
					reply.setContent(comment.getContent());
					reply.setReplyUserId(comment.getReply_user_id());
					List<ThumbUp> thumbs = thumbUpDao.findThumbUpByAssociatedTypeAndAssociatedId(ThumbUpType.STATIONREPLY.getValue(), comment.getReply_id());
				
					reply.setThumbUpNum(thumbs.size());
					
					//查询用户信息
					LOG.info("获取到评论的用户id="+comment.getUser_id());
					UserInfo userInfo = userInfoDao.findUserByUserId(comment.getUser_id());
					UserInfo replyUserInfo = userInfoDao.findUserByUserId(comment.getReply_user_id());
					if(userInfo==null||replyUserInfo==null){			
						LOG.error("获取到评论的用户为空，评论用户的userid为="+comment.getUser_id());
						LOG.error("获取到评论的用户为空，评论用户的userid为="+comment.getReply_user_id());
						continue;
					}
								
					//展示用户V
					UserBaseVo userBaseVo = new UserBaseVo();
					
					userBaseVo = setUserbasVo(userInfo);
					reply.setUser(userBaseVo);
					reply.setAuditFlag(comment.getValid_flag().toString());
					
					UserBaseVo replyUserBaseVo = new UserBaseVo();
					
					replyUserBaseVo = setUserbasVo(replyUserInfo);
					reply.setReplyUser(replyUserBaseVo);
					stationCommentReplyInfoVos.add(reply);
				}
		
		return stationCommentReplyInfoVos; 
	}

	public List<StationCommentReply> getStationReplyByCommentId( String commentId) {
		
		return stationCommentReplyDao.findStationCommentReplyByCommentId(commentId);
	}


	@Override
	public ResultMappingEntity getStationByCoordinate(String latitude, String longtitude, Integer number,String locaLatitude,String locaLongtitude) throws AvroRemoteException {
		
		LOG.info("按条件读取站点信息，经度["+longtitude+"]纬度【"+latitude+"】，获取数量["+number+"]");		
		// 获取站点详细
		String resultJsonString = omsService.getStationByLocation(longtitude,latitude,number+"").toString();
		LOG.info("oms返回数据"+resultJsonString);
		LOG.info("----------------");
		return getStationDetailByJson(resultJsonString,locaLatitude,locaLongtitude);
	}


	@Override 
	public ResultMappingEntity getStationByName(String name, String cityCode,String latitude, String longtitude) throws AvroRemoteException {
		
		LOG.info("按条件读取站点信息，名称["+name+"]城市代码【"+cityCode+"】");

		String resultJsonString = omsService.getStationByName(name,cityCode).toString();
		LOG.info("oms返回数据"+resultJsonString);
		LOG.info("----------------");
		return getStationDetailByJson(resultJsonString,latitude,longtitude);

	}
	/**
	 * 
	 * @param resultJsonString oms 返回结果
	 * @param latitude 纬度
	 * @param longtitude 经度
	 * @throws AvroRemoteException
	 */
	private ResultMappingEntity  getStationDetailByJson(String resultJsonString,String latitude, String longtitude) throws AvroRemoteException{
		
		List<StationDetailVo> stationDetailVos =new ArrayList<StationDetailVo>();
		// 数据转换
				JSONObject omsResultObject = JSONObject.parseObject(resultJsonString);
				// 获取code
				String code = omsResultObject.getString(Constants.OmsServerResultField.CODE);

				if (OmsCode.SUCCESS.equals(code)) {
					// 获取站点详细
					JSONArray array = omsResultObject.getJSONArray("list");
					JSONObject obj= new JSONObject();
					// 封装站点字段
					for(int i=0 ;i<array.size();i++){
						obj = array.getJSONObject(i);
						
						StationDetailVo stationDetailVo = JsonUtils.JsonToObjectOfClass(obj, StationDetailVo.class);
						
						stationDetailVo.setLatitude(stationDetailVo.getCoordinate_y());
						stationDetailVo.setLongitude(stationDetailVo.getCoordinate_x());
						stationDetailVo.setCoordinate_x(null);
						stationDetailVo.setCoordinate_y(null);
						
						if(stationDetailVo.getScore()==null||stationDetailVo.getScore().length()==0){
							stationDetailVo.setScore("5");
						}
						stationDetailVo.setGrade(stationDetailVo.getScore());
						//计算经纬度
						if(tt.dz.appserver.util.StringUtils.isNotBlank(stationDetailVo.getLatitude())&&
							tt.dz.appserver.util.StringUtils.isNotBlank(stationDetailVo.getLongitude())&&
							tt.dz.appserver.util.StringUtils.isNotBlank(latitude)&&
							tt.dz.appserver.util.StringUtils.isNotBlank(longtitude)){
							
							double distance= CalculateUtil.GetDistance(Double.parseDouble(latitude), Double.parseDouble(longtitude),
									Double.parseDouble(stationDetailVo.getLatitude()),Double.parseDouble(stationDetailVo.getLongitude()));
							stationDetailVo.setDistance(String.valueOf(distance));
						}
						stationDetailVo.setChargePortType("0001");
						
						stationDetailVos.add(stationDetailVo);
					}
					return 	 Result.responseBody(Response.SUCCESS.getCode(), Response.SUCCESS.getMsg(),stationDetailVos);	
				}
					return 	 Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg());
	}


	@Override
	public ResultMappingEntity collectionStationList(String userId,Integer pageNo ) throws AvroRemoteException {
		List<StationCollection>	 	collectionStations=stationCollectionDao.findCollectionStationByUserId(userId,pageNo);
		List<StationCollectionVO>  list= new 	ArrayList<StationCollectionVO>();
			if(collectionStations!=null){
				for(StationCollection coll:collectionStations){
					StationCollectionVO vo= new StationCollectionVO();
					vo.setUserId(coll.getUser_id());
					vo.setStationId(coll.getStation_id());
					vo.setStationName(coll.getStation_name());
					vo.setStationAddr(coll.getStation_addr());
					vo.setCollTime(DateUtil.getDateTime(coll.getColl_time()));
					if(coll.getLatitude()!=null&&coll.getLongitude()!=null){
						vo.setLatitude(String.valueOf(coll.getLatitude()));
						vo.setLongitude(String.valueOf(coll.getLongitude()));
					}
					list.add(vo);
				}
			}		
		return  Result.responseBody(Response.SUCCESS.getCode(), Response.SUCCESS.getMsg(),list);	
	}
	
	private static UserBaseVo setUserbasVo(UserInfo userInfo){
		
		UserBaseVo userBaseVo = new UserBaseVo();
		
		userBaseVo.setUserId(userInfo.getUser_id());
		if(userInfo!=null){
			
			if(userInfo.getNickname()!=null){
				userBaseVo.setNickName(userInfo.getNickname());
			} else{	
				userBaseVo.setNickName(userInfo.getTelephone().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
			}	
			
			userBaseVo.setSex(userInfo.getSex().toString());
			userBaseVo.setSignature(userInfo.getSignature());
			userBaseVo.setPhone(userInfo.getTelephone());			
			//用户头像
			userBaseVo.setHeadImage(userInfo.getHead_path());
		
		}
		return  userBaseVo;
		
	}
	
	/**
	 * 通过站获取费率
	 * @param stationId 站点id
	 * 通过站点id查询站点下所有的装桩， 查询所有桩的费率， 取装的费率为站的费率
	 */
	private static void getRateByStationId(String stationId){
		
		//查询所以桩的信息
		//查询单个桩的费率
		//获取一个最大的充电费，最小的充电费；格式  xxx-xxx元/度，xxx-xxx元/次
		//获取服务费 格式  xxx-xxx元/度，xxx-xxx元/次
		
		
	}
	
}
	

